#include "../include/appslist.h"

AppsList::AppsList() {
	this->updateApps();
}

void AppsList::updateApps() {
	this->apps.clear();

	string path = "/usr/share/applications";
	for (const auto &entry : filesystem::directory_iterator(path)) {
		mINI::INIFile ifile(entry.path());
		mINI::INIStructure ini;

		ifile.read(ini);

		string &name = ini["Desktop Entry"]["Name"];
		string &exec = ini["Desktop Entry"]["Exec"];
		string &noDisplayStr = ini["Desktop Entry"]["NoDisplay"];
		string &isTerminalStr = ini["Desktop Entry"]["Terminal"];

		bool noDisplay = stringToBool(noDisplayStr);
		bool isTerminal = stringToBool(isTerminalStr);

		if (noDisplay || name == "") continue;
		if (isTerminal) exec = getTerminal() + " -e " + exec;

		this->apps[name] = exec;
	}
}

void AppsList::printAppNames() {
	for (const auto &appItem : this->apps) {
		cout << appItem.first << endl;
	}
}
